package com.platform.modules.wallet.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.platform.common.constant.ApiConstant;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatFriend;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatFriendService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.push.enums.PushMsgTypeEnum;
import com.platform.modules.push.service.ChatPushService;
import com.platform.modules.push.vo.PushParamVo;
import com.platform.modules.wallet.domain.WalletTransfer;
import com.platform.modules.wallet.service.WalletTransferService;
import com.platform.modules.wallet.vo.TransferVo02;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 钱包-转账
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class TransferController {


    @Resource
    private WalletTransferService walletTransferService;
    @Resource
    private ChatUserService chatUserService;
    @Resource
    private ChatPushService chatPushService;

    /**
     * 查询转账信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getTransfer/{queryId}")
    public AjaxResult getTransfer(@PathVariable String queryId) {

        WalletTransfer transfer = walletTransferService.getByQueryId(queryId);
        return AjaxResult.success(transfer);
    }


    /**
     * 转账接收
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/transferReceive")
    public AjaxResult transferReceive(@Validated @RequestBody TransferVo02 vo) {


        Long userId = ShiroUtils.getUserId();

        walletTransferService.receive(vo.getId(),userId);

        //TODO 下面先这么做着 以后再封装优化
        WalletTransfer transfer = walletTransferService.getByQueryId(vo.getId());

        ChatUser to = chatUserService.getById(transfer.getToId());
        String content = StrUtil.format(ApiConstant.TRANSFER_RECEIVE_NOTICE, to.getNickName());
        // 发送通知
        chatPushService.pushMsg(ChatUser.initParam(to)
                .setContent(content).setToId(transfer.getFromId()), PushMsgTypeEnum.ALERT);

        ChatUser from = chatUserService.getById(transfer.getFromId());
        chatPushService.pushMsg(ChatUser.initParam(from).setContent(content).setToId(transfer.getToId()), PushMsgTypeEnum.ALERT);

        //{"amount":"1.13","transferId":1552841919248732161,"inStatus":0}
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.set("amount",new BigDecimal(transfer.getAmount().intValue()).divide(new BigDecimal(100)).toString());
//        jsonObject.set("transferId",transfer.getId());
//        jsonObject.set("inStatus",transfer.getInStatus().getCode());
//
//        ChatFriend friend2 = friendService.getFriend(transfer.getToId(), transfer.getFromId());
//        PushParamVo paramVo = ChatUser.initParam(chatUserService.getById(transfer.getFromId()))
//                .setNickName(friend2.getRemark())
//                .setTop(friend2.getTop())
//                .setContent(jsonObject.toString());
//        // 推送
//        chatPushService.pushMsg(paramVo.setToId(transfer.getToId()), PushMsgTypeEnum.TRANSFER);
//
//        friend2 = friendService.getFriend(transfer.getFromId(), transfer.getToId());
//        paramVo = ChatUser.initParam(chatUserService.getById(transfer.getToId()))
//                .setNickName(friend2.getRemark())
//                .setTop(friend2.getTop())
//                .setContent(jsonObject.toString());
//        // 推送
//        chatPushService.pushMsg(paramVo.setToId(transfer.getFromId()), PushMsgTypeEnum.TRANSFER);

        return AjaxResult.success("收款成功");
    }


}
